<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  <title>
    
    Hexo
  </title>
  <!-- Icon -->
  
    <link rel="shortcut icon" href="/favicon.ico">
    
  
<link rel="stylesheet" href="/css/style.css">

  
  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<script src="/js/pace.min.js"></script>

<meta name="generator" content="Hexo 6.0.0"></head>

<body>
  <main class="content">
    <section class="jumbotron">
  <div class="video">
    
    <div class="video-frame">
      <img src="/images/ocean/overlay-hero.png" alt="Decorative image frame">
    </div>
    
    <div class="video-media">
      <video playsinline="" autoplay="" loop="" muted="" data-autoplay="" poster="/images/ocean/ocean.png"
        x5-video-player-type="h5">
        <source src="/images/ocean/ocean.mp4" type="video/mp4">
        <source src="/images/ocean/ocean.ogv" type="video/ogg">
        <source src="/images/ocean/ocean.webm" type="video/webm">
        <p>Your user agent does not support the HTML5 Video element.</p>
      </video>
      <div class="video-overlay"></div>
    </div>
    <div class="video-inner text-center text-white">
      <h1><a href="/">Hexo</a></h1>
      <p></p>
      <div><img src="/images/hexo-inverted.svg" class="brand" alt="Hexo"></div>
    </div>
    <div class="video-learn-more">
      <a class="anchor" href="#landingpage"><i class="fe fe-mouse"></i></a>
    </div>
  </div>
</section>
<div id="landingpage">
  <section class="outer">
  <article class="articles">
    
    <h1 class="page-type-title"></h1>
    
    
    <article id="post-multisearch" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
      
  
  <h2 class="article-title" itemprop="name">
    <a href="/2022/09/10/multisearch/">multisearch</a>
  </h2>
  
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2022/09/10/multisearch/" class="article-date">
  <time datetime="2022-09-09T17:17:40.000Z" itemprop="datePublished">2022-09-10</time>
</a>
      
    </div>
    

    

    <div class="article-entry" itemprop="articleBody">
      
      
        
      
      
        <h1 id="联合查询"><a href="#联合查询" class="headerlink" title="联合查询"></a>联合查询</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><blockquote>
<p>联合查询是可合并多个相似的选择查询的结果集。等同于将一个表追加到另一个表，从而实现将多张表的查询组合到一起。</p>
</blockquote>
<h2 id="前置知识"><a href="#前置知识" class="headerlink" title="前置知识"></a>前置知识</h2><h3 id="SQL-JOIN"><a href="#SQL-JOIN" class="headerlink" title="SQL JOIN"></a>SQL JOIN</h3><p><strong>SQL join 用于根据两个或多个表中的列之间的关系，从这些表中查询数据。</strong></p>
<p>SQL JOIN有</p>
<ul>
<li><p>JOIN：如果表中有至少一个匹配，则返回行</p>
</li>
<li><p>INNNER JOIN：在表中存在至少一个匹配时，INNER JOIN 关键字返回行</p>
</li>
<li><p>LEFT JOIN：即使右表中没有匹配，也从左表返回所有的行</p>
</li>
<li><p>RIGHT JOIN：即使左表中没有匹配，也从右表返回所有的行</p>
</li>
<li><p>FULL JOIN（MYSQL不支持）：只要其中一个表中存在匹配，就返回行</p>
</li>
</ul>
<p><img src="https://s1.328888.xyz/2022/08/23/bnnEE.png"></p>
<p>实例：</p>
<p>“Person”表：</p>
<table>
<thead>
<tr>
<th align="left">Id_P</th>
<th align="left">LastName</th>
<th align="left">FirstName</th>
<th align="left">Address</th>
<th align="left">City</th>
</tr>
</thead>
<tbody><tr>
<td align="left">1</td>
<td align="left">Adams</td>
<td align="left">John</td>
<td align="left">Oxford Street</td>
<td align="left">London</td>
</tr>
<tr>
<td align="left">2</td>
<td align="left">Bush</td>
<td align="left">George</td>
<td align="left">Fifth Avenue</td>
<td align="left">New York</td>
</tr>
<tr>
<td align="left">3</td>
<td align="left">Carter</td>
<td align="left">Thomas</td>
<td align="left">Changan Street</td>
<td align="left">Beijing</td>
</tr>
</tbody></table>
<p>“Orders” 表：</p>
<table>
<thead>
<tr>
<th align="left">Id_O</th>
<th align="left">OrderNo</th>
<th align="left">Id_P</th>
</tr>
</thead>
<tbody><tr>
<td align="left">1</td>
<td align="left">77895</td>
<td align="left">3</td>
</tr>
<tr>
<td align="left">2</td>
<td align="left">44678</td>
<td align="left">3</td>
</tr>
<tr>
<td align="left">3</td>
<td align="left">22456</td>
<td align="left">1</td>
</tr>
<tr>
<td align="left">4</td>
<td align="left">24562</td>
<td align="left">1</td>
</tr>
<tr>
<td align="left">5</td>
<td align="left">34764</td>
<td align="left">65</td>
</tr>
</tbody></table>
<p>请注意，”Id_O” 列是 Orders 表中的的主键，同时，”Orders” 表中的 “Id_P” 列用于引用 “Persons” 表中的人，而无需使用他们的确切姓名。</p>
<p>请留意，”Id_P” 列把上面的两个表联系了起来。</p>
<p>我们应该怎么样得知谁订购了产品，并且他们订购了什么产品呢？</p>
<p>如果我们希望列出所有人的定购，可以使用下面的 SELECT 语句：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> Persons.LastName, Persons.FirstName, Orders.OrderNo</span><br><span class="line"><span class="keyword">FROM</span> Persons</span><br><span class="line"><span class="keyword">INNER</span> <span class="keyword">JOIN</span> Orders</span><br><span class="line"><span class="keyword">ON</span> Persons.Id_P <span class="operator">=</span> Orders.Id_P</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> Persons.LastName</span><br></pre></td></tr></table></figure>

<h3 id="association标签"><a href="#association标签" class="headerlink" title="association标签"></a>association标签</h3><p>该标签用来实现<strong>一对一</strong>与<strong>多对一</strong>映射</p>
<ol>
<li>association标签中的property：many的一方的实体类中，添加的属性名。</li>
<li>association标签中的javaType：many的一方的实体类中，添加的属性类型。</li>
<li>id标签和result标签中的property：one的一方的实体类中的属性名。</li>
<li>id标签和result标签中的column：one的一方的，查询出来的字段名。</li>
</ol>
<h3 id="collection标签"><a href="#collection标签" class="headerlink" title="collection标签"></a>collection标签</h3><p>该标签用来实现<strong>一对多</strong>与<strong>多对多</strong>映射</p>
<ol>
<li>collection标签中的property：one的一方的实体类中，添加的集合属性名。</li>
<li>collection标签中的ofType：one的一方的实体类中，添加的集合中的元素类型。</li>
<li>id标签和result标签中的property：many的一方的实体类中的属性名。</li>
<li>id标签和result标签中的column： many的一方的，查询出来的字段名。</li>
</ol>
<h2 id="联合查询的三种类型（举例子）"><a href="#联合查询的三种类型（举例子）" class="headerlink" title="联合查询的三种类型（举例子）"></a>联合查询的三种类型（举例子）</h2><h3 id="一对一"><a href="#一对一" class="headerlink" title="一对一"></a>一对一</h3><ul>
<li><strong>demo应用场景：</strong>从<strong>学生基本信息表student</strong>关联查询出<strong>学生详细信息表studentDetail</strong>（通过学生<strong>id</strong>关联）</li>
<li>方法：<strong>组合VO实体类查询</strong></li>
</ul>
<ol>
<li>在任意”一“的一方<strong>student</strong>中创建<strong>studentDetail</strong>的单个对象</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 学生详细列表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> StudentDetail studentDetailList;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>新建一个<strong>VO</strong>实体类：组合<strong>student</strong>和<strong>studentDetail</strong>中的属性</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> cn.codnoy.springboot.examples.dto;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.Serializable;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentDetailVO</span> <span class="keyword">implements</span> <span class="title class_">Serializable</span> &#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * serialVersionUID</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">long</span> <span class="variable">serialVersionUID</span> <span class="operator">=</span> <span class="number">1L</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * ID</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> Long id;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 班级No</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String classNo;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 所属学院</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String collegeName;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 所属专业</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String majorName;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 学号</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String studentId;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 姓名</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 性别 男：1 女：2</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> Integer sex;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 年龄</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 个人简介</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String intro;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 状态</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> Integer status;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 入学时间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String entranceDate;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 家庭住址</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String homeAddress;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 手机</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String mobile;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * QQ号</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String qq;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 微信号</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String weixin;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 个人照片存储路径</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String picture;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建者</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String createBy;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建时间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String createTime;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 更新者</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String updateBy;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 更新时间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String updateTime;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>在student中添加一个resultmap</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">&quot;StudentAllInfoPlusResultMap&quot;</span> <span class="attr">type</span>=<span class="string">&quot;cn.codnoy.springboot.examples.dto.StudentDetailVO&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> <span class="attr">column</span>=<span class="string">&quot;id&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;classNo&quot;</span> <span class="attr">column</span>=<span class="string">&quot;class_no&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;collegeName&quot;</span> <span class="attr">column</span>=<span class="string">&quot;college_name&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;majorName&quot;</span> <span class="attr">column</span>=<span class="string">&quot;major_name&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;studentId&quot;</span> <span class="attr">column</span>=<span class="string">&quot;student_id&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;name&quot;</span> <span class="attr">column</span>=<span class="string">&quot;name&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;sex&quot;</span> <span class="attr">column</span>=<span class="string">&quot;sex&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;age&quot;</span> <span class="attr">column</span>=<span class="string">&quot;age&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;status&quot;</span> <span class="attr">column</span>=<span class="string">&quot;status&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;intro&quot;</span> <span class="attr">column</span>=<span class="string">&quot;intro&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;entranceDate&quot;</span> <span class="attr">column</span>=<span class="string">&quot;entrance_date&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;homeAddress&quot;</span> <span class="attr">column</span>=<span class="string">&quot;home_address&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;mobile&quot;</span> <span class="attr">column</span>=<span class="string">&quot;mobile&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;qq&quot;</span> <span class="attr">column</span>=<span class="string">&quot;qq&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;weixin&quot;</span> <span class="attr">column</span>=<span class="string">&quot;weixin&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;picture&quot;</span> <span class="attr">column</span>=<span class="string">&quot;picture&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;createBy&quot;</span> <span class="attr">column</span>=<span class="string">&quot;create_by&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;createTime&quot;</span> <span class="attr">column</span>=<span class="string">&quot;create_time&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;updateBy&quot;</span> <span class="attr">column</span>=<span class="string">&quot;update_by&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;updateTime&quot;</span> <span class="attr">column</span>=<span class="string">&quot;update_time&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li>在select中使用join实现关联查询</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">&quot;selectAllFieldsPlus&quot;</span>&gt;</span></span><br><span class="line">    select</span><br><span class="line">        student.id,</span><br><span class="line">        student.class_no,</span><br><span class="line">        student.college_name,</span><br><span class="line">        student.major_name,</span><br><span class="line">        student.student_id,</span><br><span class="line">        student.name,</span><br><span class="line">        student.sex,</span><br><span class="line">        student.age,</span><br><span class="line">        student.intro,</span><br><span class="line">        student.status,</span><br><span class="line">        student.entrance_date,</span><br><span class="line">        student_detail.home_address,</span><br><span class="line">        student_detail.mobile,</span><br><span class="line">        student_detail.qq,</span><br><span class="line">        student_detail.weixin,</span><br><span class="line">        student_detail.picture,</span><br><span class="line">        student_detail.create_by,</span><br><span class="line">        student_detail.create_time,</span><br><span class="line">        student_detail.update_by,</span><br><span class="line">        student_detail.update_time</span><br><span class="line">    from student</span><br><span class="line">    left join student_detail</span><br><span class="line">    on student.id = student_detail.id</span><br><span class="line"><span class="tag">&lt;/<span class="name">sql</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getStudentAllByIdPlus&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;String&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;StudentAllInfoPlusResultMap&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">&quot;selectAllFieldsPlus&quot;</span>/&gt;</span></span><br><span class="line">    where student.class_no = #&#123;classNo&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="5">
<li>应用<strong>Spring Boot</strong>三层架构，调用<strong>student</strong>中<strong>getStudentAllByIdPlus</strong>方法联合查询两表数据</li>
</ol>
<h3 id="一对多"><a href="#一对多" class="headerlink" title="一对多"></a>一对多</h3><ul>
<li><strong>demo应用场景：</strong>从<strong>套餐表Orders</strong>关联查询出<strong>套餐明细表SetmealDetail</strong>（每个<strong>套餐明细表</strong>中都有表示<strong>套餐类型</strong>的数字——**<code>smId</code>**）</li>
<li>方法：<strong>单独查询</strong></li>
</ul>
<ol>
<li>在<strong>“一”</strong>的里面创建“多”的<strong>列表</strong></li>
</ol>
<p>在<strong>setmeal</strong>套餐中创建<strong>SetmealDetail</strong>对象的列表</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> List&lt;SetmealDetailed&gt; sdList;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><p>在<strong>“一”</strong>的<strong>xml</strong>里面添加对应的<strong>resultmap映射 和 select语句</strong></p>
<ul>
<li>resultmap中<strong>“多”</strong>的属性外使用**<code>&lt;collection&gt;</code><strong>标签，表明</strong>关联表**的属性</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">&quot;setmealResultMap&quot;</span> <span class="attr">type</span>=<span class="string">&quot;Setmeal&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;smId&quot;</span> <span class="attr">property</span>=<span class="string">&quot;smId&quot;</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;name&quot;</span> <span class="attr">property</span>=<span class="string">&quot;name&quot;</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;type&quot;</span> <span class="attr">property</span>=<span class="string">&quot;type&quot;</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;price&quot;</span> <span class="attr">property</span>=<span class="string">&quot;price&quot;</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">&quot;sdList&quot;</span> <span class="attr">ofType</span>=<span class="string">&quot;SetmealDetailed&quot;</span></span></span><br><span class="line"><span class="tag">      <span class="attr">select</span>=<span class="string">&quot;com.neusoft.tijian.mapper.SetmealDetailedMapper.listSetmealDetailedBySmId&quot;</span> <span class="attr">column</span>=<span class="string">&quot;smId&quot;</span>&gt;</span>         <span class="tag">&lt;/<span class="name">collection</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<ol>
<li><strong>resultmap：</strong></li>
</ol>
<p><strong>column</strong>：数据库中的字段；                            <strong>property</strong>：java代码中的属性；</p>
<ol start="2">
<li><strong>collection：</strong></li>
</ol>
<p><strong>ofType</strong>：关联表的实体对象(Model &#x2F; Po)；    <strong>select</strong>：单独查询 (关联的两张表，每张表中各自写自己的<strong>sql</strong>查询语句)</p>
<p><strong>property：</strong>新建列表的属性名;                         <strong>column：</strong>关联两张表的值(共有)</p>
</blockquote>
<ul>
<li>select语句用<strong>join</strong>实现表的<strong>联合查询</strong></li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;listSetmealBySmId&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;setmealResultMap&quot;</span>&gt;</span></span><br><span class="line">      select * from setmeal where smid=#&#123;smId&#125; order by smId</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在<strong>“多”</strong>的<strong>“xml”</strong>里面添加对应的<strong>select语句</strong></p>
</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;listSetmealDetailedBySmId&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;setmealDetailedResultMap&quot;</span>&gt;</span></span><br><span class="line">      select * from setmealDetailed where smid=#&#123;smId&#125; order by sdId</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li>应用<strong>Spring Boot</strong>三层架构，调用<strong>“一”setmeal</strong>中<strong>listSetmealBySmId</strong>方法联合查询两表数据</li>
</ol>
<h3 id="多对一"><a href="#多对一" class="headerlink" title="多对一"></a>多对一</h3><ul>
<li><strong>demo应用场景：</strong>从<strong>订单表Orders</strong>关联出<strong>套餐表Setmeal</strong>的查询（每个<strong>订单</strong>中都有一个反映<strong>套餐类型</strong>的数字——<strong>smId</strong>）</li>
<li>方法：<strong>单独查询</strong></li>
</ul>
<ol>
<li>在<strong>“多”</strong>的里面创建“一”的<strong>单个对象</strong></li>
</ol>
<p>在<strong>“Orders”</strong>套餐明细中创建<strong>”setmeal“</strong>的一个实体对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> Setmeal setmeal;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><p>在<strong>“多”</strong>的xml里面添加对应的<strong>resultmap映射 和 select语句</strong></p>
<ul>
<li>resultmap中<strong>“一”</strong>的属性外使用**<code>&lt;association&gt;</code><strong>标签，表明</strong>关联表**的属性</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">&quot;OrdersResultMap&quot;</span> <span class="attr">type</span>=<span class="string">&quot;Orders&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;orderId&quot;</span> <span class="attr">property</span>=<span class="string">&quot;orderId&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;orderDate&quot;</span> <span class="attr">property</span>=<span class="string">&quot;orderDate&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;userId&quot;</span> <span class="attr">property</span>=<span class="string">&quot;userId&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;hpId&quot;</span> <span class="attr">property</span>=<span class="string">&quot;hpId&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;smId&quot;</span> <span class="attr">property</span>=<span class="string">&quot;smId&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;state&quot;</span> <span class="attr">property</span>=<span class="string">&quot;state&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">&quot;setmeal&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;Setmeal&quot;</span></span></span><br><span class="line"><span class="tag">                 <span class="attr">select</span>=<span class="string">&quot;com.neusoft.tijian.mapper.SetmealMapper.listSetmealBySmId&quot;</span> <span class="attr">column</span>=<span class="string">&quot;smId&quot;</span>&gt;</span>           <span class="tag">&lt;/<span class="name">association</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<ol>
<li><strong>resultmap：</strong></li>
</ol>
<p><strong>column</strong>：数据库中的字段；                            <strong>property</strong>：java代码中的属性；</p>
<ol start="2">
<li><strong>collection：</strong></li>
</ol>
<p><strong>javaType</strong>：关联表的实体对象(Model &#x2F; Po)；    <strong>select</strong>：单独查询另一关联表中的方法(每张表中写各自的<strong>sql</strong>查询语句)</p>
<p><strong>property：</strong>新建列表的属性名;                             <strong>column：</strong>关联两张表的值(共有)</p>
</blockquote>
<ul>
<li>select语句用<strong>join</strong>实现表的<strong>联合查询</strong></li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;listOrdersByUserId&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;String&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;OrdersResultMap&quot;</span>&gt;</span></span><br><span class="line">    select * from orders where userid=#&#123;userId&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在<strong>“一”</strong>的<strong>“xml”</strong>里面添加对应的<strong>select语句</strong></p>
</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;listSetmealBySmId&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;setmealResultMap&quot;</span>&gt;</span></span><br><span class="line">     select * from setmeal where smid=#&#123;smId&#125; order by smId</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li>应用<strong>Spring Boot</strong>三层架构，调用<strong>”多“Orders</strong>中方法<strong>listOrdersByUserId</strong>联合查询两表数据</li>
</ol>
<h2 id="联合查询的实现方法"><a href="#联合查询的实现方法" class="headerlink" title="联合查询的实现方法"></a>联合查询的实现方法</h2><h3 id="多表连接形式"><a href="#多表连接形式" class="headerlink" title="多表连接形式"></a>多表连接形式</h3><h4 id="VO新建dto重组表中属性-SQL-JOIN（一对一关联查询）"><a href="#VO新建dto重组表中属性-SQL-JOIN（一对一关联查询）" class="headerlink" title="VO新建dto重组表中属性 + SQL JOIN（一对一关联查询）"></a>VO新建dto重组表中属性 + SQL JOIN（一对一关联查询）</h4><ol>
<li><p>按需新建dto</p>
<p><img src="https://s1.328888.xyz/2022/09/06/5LYhj.png"></p>
<p>笔者新建的这个Student_StudentScoreVO，拥有Student和StudentScore的所有属性。</p>
</li>
<li><p>在xml映射文件中编写resultMap</p>
<p><img src="https://s1.328888.xyz/2022/09/06/5LvNE.png"></p>
<p>相信大家对编写resultMap都很熟悉，笔者就不再赘述了。</p>
</li>
<li><p>编写sql片段标签标签</p>
<p><img src="https://s1.328888.xyz/2022/09/06/5N3i5.png"></p>
<p>这个语句的意思：通过学生ID联合student表和student_score表（student表作为主表），选取所有学生基本信息和成绩信息。</p>
</li>
<li><p>编写select语句</p>
<p><img src="https://s1.328888.xyz/2022/09/06/5NkcU.png"></p>
<p>这条语句的作用，我不用解释了吧。。。。</p>
</li>
</ol>
<h4 id="association（一对一关联查询）"><a href="#association（一对一关联查询）" class="headerlink" title="association（一对一关联查询）"></a>association（一对一关联查询）</h4><ol>
<li><p>创建实体类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> id;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> sex;</span><br><span class="line">    <span class="keyword">private</span> StudentCard studentCard; <span class="comment">//添加 StudentCard 至 Student</span></span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentCard</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> id;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> studentId;</span><br><span class="line">    <span class="keyword">private</span> Date startDate;</span><br><span class="line">    <span class="keyword">private</span> Date endDate;</span><br><span class="line">   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>写xml文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">&quot;net.biancheng.po.Student&quot;</span> <span class="attr">id</span>=<span class="string">&quot;cardAndStu2&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> <span class="attr">column</span>=<span class="string">&quot;id&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;name&quot;</span> <span class="attr">column</span>=<span class="string">&quot;name&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;sex&quot;</span> <span class="attr">column</span>=<span class="string">&quot;sex&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">&quot;studentCard&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">javaType</span>=<span class="string">&quot;net.biancheng.po.StudentCard&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> <span class="attr">column</span>=<span class="string">&quot;id&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;studentId&quot;</span> <span class="attr">column</span>=<span class="string">&quot;studentId&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">association</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectStuById2&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Integer&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">resultMap</span>=<span class="string">&quot;cardAndStu2&quot;</span>&gt;</span></span><br><span class="line">    SELECT </span><br><span class="line">    	s.*,</span><br><span class="line">    	sc.studentId </span><br><span class="line">    FROM student s,studentCard sc</span><br><span class="line">    WHERE</span><br><span class="line">    s.cardId = sc.id AND s.id=#&#123;id&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="collection-SQL-JOIN（一对多关联查询）"><a href="#collection-SQL-JOIN（一对多关联查询）" class="headerlink" title="collection + SQL JOIN（一对多关联查询）"></a>collection + SQL JOIN（一对多关联查询）</h4><ol>
<li><p>修改实体类</p>
<p><img src="https://s1.328888.xyz/2022/08/23/bRdms.jpg"></p>
<p>Dept是one的一方，Emp是many的一方； 在one的一方添加many的一方的集合，这就配置了实体类<br>之间的一对多关联。</p>
</li>
<li><p>使用resultMap映射关联</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">&quot;Dept&quot;</span> <span class="attr">id</span>=<span class="string">&quot;deptResultMap&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;deptno&quot;</span> <span class="attr">column</span>=<span class="string">&quot;deptno&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;dname&quot;</span> <span class="attr">column</span>=<span class="string">&quot;dname&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;loc&quot;</span> <span class="attr">column</span>=<span class="string">&quot;loc&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">&quot;empList&quot;</span> <span class="attr">ofType</span>=<span class="string">&quot;Emp&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;empno&quot;</span> <span class="attr">column</span>=<span class="string">&quot;eempno&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;ename&quot;</span> <span class="attr">column</span>=<span class="string">&quot;eename&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;job&quot;</span> <span class="attr">column</span>=<span class="string">&quot;ejob&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;hiredate&quot;</span> <span class="attr">column</span>=<span class="string">&quot;ehiredate&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;sal&quot;</span> <span class="attr">column</span>=<span class="string">&quot;esal&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;deptno&quot;</span> <span class="attr">column</span>=<span class="string">&quot;edeptno&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">collection</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getDeptById&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;deptResultMap&quot;</span>&gt;</span></span><br><span class="line">	select d.*,</span><br><span class="line">		   e.empno eempno,</span><br><span class="line">	       e.ename eename,</span><br><span class="line">		   e.job ejob,</span><br><span class="line">		   e.hiredate ehiredate,</span><br><span class="line">		   e.sal esal,</span><br><span class="line">		   e.deptno edeptno</span><br><span class="line">	from dept d left join emp e</span><br><span class="line">	     on d.deptno=e.deptno</span><br><span class="line">	where d.deptno = #&#123;deptno&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;listDeptAll&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;deptResultMap&quot;</span>&gt;</span></span><br><span class="line">	select d.*,</span><br><span class="line">		   e.empno eempno,</span><br><span class="line">	 	   e.ename eename,</span><br><span class="line">		   e.job ejob,</span><br><span class="line">		   e.hiredate ehiredate,</span><br><span class="line">		   e.sal esal,</span><br><span class="line">		   e.deptno edeptno</span><br><span class="line">	from dept d left join emp e</span><br><span class="line">		 on d.deptno=e.deptno</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="association-SQL-JOIN（多对一关联查询）"><a href="#association-SQL-JOIN（多对一关联查询）" class="headerlink" title="association + SQL JOIN（多对一关联查询）"></a>association + SQL JOIN（多对一关联查询）</h4><ol>
<li><p>修改实体类</p>
<p><img src="https://s1.328888.xyz/2022/08/23/bRHtk.jpg"></p>
<p>Dept是one的一方，Emp是many的一方； 在many的一方添加one的一方的对象，这就配置了实体类之间的多对一关联。</p>
</li>
<li><p>使用resultMap映射关联</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">&quot;Emp&quot;</span> <span class="attr">id</span>=<span class="string">&quot;empResultMap&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;empno&quot;</span> <span class="attr">column</span>=<span class="string">&quot;empno&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;ename&quot;</span> <span class="attr">column</span>=<span class="string">&quot;ename&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;job&quot;</span> <span class="attr">column</span>=<span class="string">&quot;job&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;hiredate&quot;</span> <span class="attr">column</span>=<span class="string">&quot;hiredate&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;sal&quot;</span> <span class="attr">column</span>=<span class="string">&quot;sal&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;deptno&quot;</span> <span class="attr">column</span>=<span class="string">&quot;deptno&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">&quot;dept&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;Dept&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;deptno&quot;</span> <span class="attr">column</span>=<span class="string">&quot;ddeptno&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;dname&quot;</span> <span class="attr">column</span>=<span class="string">&quot;ddname&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;loc&quot;</span> <span class="attr">column</span>=<span class="string">&quot;dloc&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">association</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getEmpById&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;empResultMap&quot;</span>&gt;</span></span><br><span class="line">	  select e.*,</span><br><span class="line">    		 d.deptno ddeptno,</span><br><span class="line">			 d.dname ddname,</span><br><span class="line">			 d.loc dloc</span><br><span class="line">	  from emp e left join dept d</span><br><span class="line">		   on e.deptno=d.deptno</span><br><span class="line">	  where e.empno = #&#123;empno&#125;</span><br><span class="line">  <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;listEmpAll&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;empResultMap&quot;</span>&gt;</span></span><br><span class="line">	  select e.*,</span><br><span class="line">			 d.deptno ddeptno,</span><br><span class="line">			 d.dname ddname,</span><br><span class="line">			 d.loc dloc</span><br><span class="line">	  from emp e left join dept d</span><br><span class="line">		   on e.deptno=d.deptno</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="分步查询形式"><a href="#分步查询形式" class="headerlink" title="分步查询形式"></a>分步查询形式</h3><h4 id="一对一关联查询（association）"><a href="#一对一关联查询（association）" class="headerlink" title="一对一关联查询（association）"></a>一对一关联查询（association）</h4><ol>
<li><p>创建实体类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> id;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> sex;</span><br><span class="line">    <span class="keyword">private</span> StudentCard studentCard; <span class="comment">//添加 StudentCard 至 Student</span></span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentCard</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> id;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> studentId;</span><br><span class="line">    <span class="keyword">private</span> Date startDate;</span><br><span class="line">    <span class="keyword">private</span> Date endDate;</span><br><span class="line">   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写 StudentCardMapper.xml 文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectStuCardById&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">resultType</span>=<span class="string">&quot;net.biancheng.po.StudentCard&quot;</span>&gt;</span></span><br><span class="line">	SELECT * FROM studentCard WHERE studentId = #&#123;id&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>编写 StudentMapper.xml 文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">&quot;net.biancheng.po.Student&quot;</span> <span class="attr">id</span>=<span class="string">&quot;cardAndStu1&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> <span class="attr">column</span>=<span class="string">&quot;id&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;name&quot;</span> <span class="attr">column</span>=<span class="string">&quot;name&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;sex&quot;</span> <span class="attr">column</span>=<span class="string">&quot;sex&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">&quot;studentCard&quot;</span> <span class="attr">column</span>=<span class="string">&quot;studentId&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;net.biancheng.po.StudentCard&quot;</span> <span class="attr">select</span>=<span class="string">&quot;net.biancheng.mapper.StudentCardMapper.selectStuCardById&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line">    </span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectStuById1&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Integer&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">resultMap</span>=<span class="string">&quot;cardAndStu1&quot;</span>&gt;</span></span><br><span class="line">	select * from student where id=#&#123;id&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="一对多关联查询（collection）"><a href="#一对多关联查询（collection）" class="headerlink" title="一对多关联查询（collection）"></a>一对多关联查询（collection）</h4><ol>
<li><p>先在many的一方添加关联查询</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getEmpByIdLazy&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;Emp&quot;</span>&gt;</span></span><br><span class="line">	select * from emp where deptno = #&#123;deptno&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>注意：此查询可以不在接口中书写响应的方法</p>
</li>
<li><p>one的一方添加查询</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">&quot;Dept&quot;</span> <span class="attr">id</span>=<span class="string">&quot;deptResultMap&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;deptno&quot;</span> <span class="attr">column</span>=<span class="string">&quot;deptno&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;dname&quot;</span> <span class="attr">column</span>=<span class="string">&quot;dname&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;loc&quot;</span> <span class="attr">column</span>=<span class="string">&quot;loc&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">&quot;empList&quot;</span> <span class="attr">ofType</span>=<span class="string">&quot;Emp&quot;</span></span></span><br><span class="line"><span class="tag">		   <span class="attr">select</span>=<span class="string">&quot;com.neusoft.mapper.EmpMapper.getEmpByIdLazy&quot;</span></span></span><br><span class="line"><span class="tag"><span class="attr">column</span>=<span class="string">&quot;deptno&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getDeptById&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;deptResultMap&quot;</span>&gt;</span></span><br><span class="line">	select * from dept where deptno = #&#123;deptno&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="多对一关联查询（association）"><a href="#多对一关联查询（association）" class="headerlink" title="多对一关联查询（association）"></a>多对一关联查询（association）</h4><ol>
<li><p>先在one的一方添加关联查询</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getDeptByIdLazy&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;Dept&quot;</span>&gt;</span></span><br><span class="line">	select * from dept where deptno = #&#123;deptno&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>注意：此查询可以不在接口中书写响应的方法</p>
</li>
<li><p>many的一方添加查询</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">&quot;Emp&quot;</span> <span class="attr">id</span>=<span class="string">&quot;empResultMap&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;empno&quot;</span> <span class="attr">column</span>=<span class="string">&quot;empno&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;ename&quot;</span> <span class="attr">column</span>=<span class="string">&quot;ename&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;job&quot;</span> <span class="attr">column</span>=<span class="string">&quot;job&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;hiredate&quot;</span> <span class="attr">column</span>=<span class="string">&quot;hiredate&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;sal&quot;</span> <span class="attr">column</span>=<span class="string">&quot;sal&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;deptno&quot;</span> <span class="attr">column</span>=<span class="string">&quot;deptno&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">&quot;dept&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;Dept&quot;</span></span></span><br><span class="line"><span class="tag">		   <span class="attr">select</span>=<span class="string">&quot;com.neusoft.mapper.DeptMapper.getDeptByIdLazy&quot;</span></span></span><br><span class="line"><span class="tag"><span class="attr">column</span>=<span class="string">&quot;deptno&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getEmpById&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;empResultMap&quot;</span>&gt;</span></span><br><span class="line">	select * from emp where empno = #&#123;empno&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://zhuoshi--li.gitee.io/2022/09/10/multisearch/" data-id="cl7urk99t0004houm0a6p47fc" class="article-share-link">
        Share
      </a>
      
    </footer>

  </div>

  

  

</article>
    
    <article id="post-Mysqlnew" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
      
  
  <h2 class="article-title" itemprop="name">
    <a href="/2022/09/10/Mysqlnew/">Mysqlnew</a>
  </h2>
  
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2022/09/10/Mysqlnew/" class="article-date">
  <time datetime="2022-09-09T17:16:45.000Z" itemprop="datePublished">2022-09-10</time>
</a>
      
    </div>
    

    

    <div class="article-entry" itemprop="articleBody">
      
      
        
      
      
        <h1 id="MySQL实例"><a href="#MySQL实例" class="headerlink" title="MySQL实例"></a><code>MySQL实例</code></h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">graph TB</span><br><span class="line">    subgraph 基本操作</span><br><span class="line">	id1(增/删)--&gt; id4(用户) &amp; id5(数据库) &amp; id6(表) &amp; id7(字段及其配置)</span><br><span class="line">	id2(改)--&gt; id8(名称) &amp; id9(字段及其配置) &amp; id10(全局配置)</span><br><span class="line">	id3(查)--&gt; id11(全局信息)</span><br><span class="line">	end</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="增-x2F-删"><a href="#增-x2F-删" class="headerlink" title="增&#x2F;删"></a>增&#x2F;删</h2><h3 id="用户-x2F-数据库"><a href="#用户-x2F-数据库" class="headerlink" title="用户&#x2F;数据库"></a>用户&#x2F;数据库</h3><h4 id="1-增加用户-x2F-数据库-CREATE"><a href="#1-增加用户-x2F-数据库-CREATE" class="headerlink" title="1. 增加用户&#x2F;数据库(CREATE)"></a>1. 增加用户&#x2F;数据库(CREATE)</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">USER</span> <span class="string">&#x27;username&#x27;</span>@<span class="string">&#x27;%&#x27;</span> IDENTIFIED <span class="keyword">BY</span> <span class="string">&#x27;newgeek123&#x27;</span>;  <span class="comment">-- 创建一个用户</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">CREATE</span> DATABASE IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> team04_demo</span><br><span class="line"><span class="keyword">DEFAULT</span> <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb4</span><br><span class="line"><span class="keyword">DEFAULT</span> <span class="keyword">COLLATE</span> utf8mb4_0900_ai_ci;   <span class="comment">-- 创建一个数据库</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">GRANT</span> <span class="keyword">ALL</span> PRIVILEGES <span class="keyword">ON</span> team04_demo.<span class="operator">*</span> <span class="keyword">TO</span> <span class="string">&#x27;username&#x27;</span>@<span class="string">&#x27;%&#x27;</span> <span class="keyword">WITH</span> <span class="keyword">GRANT</span> OPTION; <span class="comment">-- 给用户使用数据库的权限</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>查看全局用户信息和权限</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&gt;</span><span class="keyword">SELECT</span> host,<span class="keyword">user</span>,Grant_priv,Super_priv <span class="keyword">FROM</span> mysql.user;</span><br></pre></td></tr></table></figure>

<p><strong>数据库定义字符集和<code>MySQL</code>配置的匹配</strong>：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&gt;</span><span class="keyword">DEFAULT</span> <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8</span><br><span class="line"><span class="operator">&gt;</span><span class="keyword">DEFAULT</span> <span class="keyword">COLLATE</span> utf8_general_ci;  <span class="comment">-- 警告字符集不匹配</span></span><br></pre></td></tr></table></figure>

<ul>
<li>查看下<code>MySQL</code>字符集</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&gt;</span><span class="keyword">SHOW</span> VARIABLES <span class="keyword">WHERE</span> Variable_name <span class="keyword">LIKE</span> <span class="string">&#x27;character\_set\_%&#x27;</span> <span class="keyword">OR</span> Variable_name <span class="keyword">LIKE</span> <span class="string">&#x27;collation%&#x27;</span>;</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&gt;</span><span class="keyword">DEFAULT</span> <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb4</span><br><span class="line"><span class="operator">&gt;</span><span class="keyword">DEFAULT</span> <span class="keyword">COLLATE</span> utf8mb4_0900_ai_ci; <span class="comment">-- 查询MySQL配置的字符集，修改当前数据库的字符集</span></span><br></pre></td></tr></table></figure>
</blockquote>
<h4 id="2-删除用户-x2F-数据库-DROP"><a href="#2-删除用户-x2F-数据库-DROP" class="headerlink" title="2. 删除用户&#x2F;数据库(DROP)"></a>2. 删除用户&#x2F;数据库(DROP)</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">user</span> username@<span class="string">&#x27;%&#x27;</span>;  <span class="comment">-- 删除用户</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">drop</span> database team04_demo; <span class="comment">-- 删除数据库 </span></span><br><span class="line"></span><br><span class="line">flush privileges;  <span class="comment">-- 刷新MySQL的系统权限相关表</span></span><br><span class="line"><span class="comment">/*新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表，否则会出现拒绝访问，还有一种方法，就是重新启动mysql服务器，来使新设置生效*/</span></span><br></pre></td></tr></table></figure>

<h3 id="表"><a href="#表" class="headerlink" title="表"></a>表</h3><h4 id="3-增加表-CREATE"><a href="#3-增加表-CREATE" class="headerlink" title="3. 增加表(CREATE)"></a>3. 增加表(CREATE)</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> student; <span class="comment">-- 提前删除已有的，防止更新替换时冲突</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> student</span><br><span class="line">(</span><br><span class="line">id <span class="type">bigint</span> <span class="keyword">not</span> <span class="keyword">null</span> auto_increment comment <span class="string">&#x27;ID&#x27;</span>,</span><br><span class="line">class_no <span class="type">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;班级No&#x27;</span>,</span><br><span class="line">college_name <span class="type">varchar</span>(<span class="number">40</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;所属学院&#x27;</span>,</span><br><span class="line">major_name <span class="type">varchar</span>(<span class="number">40</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;所属专业&#x27;</span>,</span><br><span class="line">student_id <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;学号&#x27;</span>,</span><br><span class="line">name <span class="type">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;姓名&#x27;</span>,</span><br><span class="line">sex tinyint <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;性别 男：1 ⼥：2&#x27;</span>,</span><br><span class="line">age <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;年龄&#x27;</span>,</span><br><span class="line">intro <span class="type">varchar</span>(<span class="number">5000</span>) <span class="keyword">default</span> <span class="keyword">null</span> comment <span class="string">&#x27;个⼈简介&#x27;</span>,</span><br><span class="line">entrance_date <span class="type">char</span>(<span class="number">6</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;⼊学时间&#x27;</span>,</span><br><span class="line">status tinyint <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;状态：1:未毕业 2:已毕业 3：退学 4: 休学&#x27;</span>,</span><br><span class="line">create_by <span class="type">bigint</span> <span class="keyword">default</span> <span class="keyword">null</span> comment <span class="string">&#x27;创建者&#x27;</span>,</span><br><span class="line">create_time datetime <span class="keyword">not</span> <span class="keyword">null</span> <span class="keyword">default</span> <span class="built_in">current_timestamp</span> comment <span class="string">&#x27;创建时间&#x27;</span>,</span><br><span class="line">update_by <span class="type">bigint</span> <span class="keyword">default</span> <span class="keyword">null</span> comment <span class="string">&#x27;更新者&#x27;</span>,</span><br><span class="line">update_time datetime <span class="keyword">not</span> <span class="keyword">null</span> <span class="keyword">default</span> <span class="built_in">current_timestamp</span> <span class="keyword">on</span> <span class="keyword">update</span> <span class="built_in">current_timestamp</span> comment <span class="string">&#x27;更新时间&#x27;</span>,</span><br><span class="line"><span class="keyword">primary</span> key (id)</span><br><span class="line">) engine innodb <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8mb4 comment <span class="operator">=</span> <span class="string">&#x27;学⽣基本信息表&#x27;</span>; <span class="comment">-- 最后配置的等号有无皆可</span></span><br></pre></td></tr></table></figure>

<h4 id="4-删除表-DROP"><a href="#4-删除表-DROP" class="headerlink" title="4. 删除表(DROP)"></a>4. 删除表(DROP)</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> student;</span><br></pre></td></tr></table></figure>

<h3 id="字段"><a href="#字段" class="headerlink" title="字段"></a>字段</h3><h4 id="5-增加字段及其配置-INSERT"><a href="#5-增加字段及其配置-INSERT" class="headerlink" title="5. 增加字段及其配置(INSERT)"></a>5. 增加字段及其配置(INSERT)</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student(id,class_no,college_name,major_name,student_id,name,sex,age,intro,entrance_date,status,create_by,create_time,update_by,update_time) <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;1&#x27;</span>,<span class="string">&#x27;1&#x27;</span>,<span class="number">1</span>,<span class="string">&#x27;1&#x27;</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;1&#x27;</span>,<span class="string">&#x27;1&#x27;</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;1000-01-01 00:00:00&#x27;</span>,<span class="number">1</span>,<span class="string">&#x27;9999-12-31 23:59:59&#x27;</span>); <span class="comment">-- 向数据表中插入必要的非空内容 ; 注意字段和值逐一对应</span></span><br></pre></td></tr></table></figure>

<h4 id="6-删除字段及其配置"><a href="#6-删除字段及其配置" class="headerlink" title="6. 删除字段及其配置"></a>6. 删除字段及其配置</h4><ul>
<li>删除<strong>所有</strong>表中字段**(DELETE &#x2F; TRUNCATE)**</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> student;</span><br><span class="line"><span class="keyword">truncate</span> <span class="keyword">table</span> student;  <span class="comment">-- 效率更高</span></span><br></pre></td></tr></table></figure>

<ul>
<li>删除某个表中字段名**(DROP)**</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> <span class="operator">&lt;</span>数据表名<span class="operator">&gt;</span> <span class="keyword">DROP</span> <span class="operator">&lt;</span>已有字段名<span class="operator">&gt;</span>；</span><br></pre></td></tr></table></figure>

<ul>
<li>删除表中的<strong>某一行</strong>：</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> <span class="operator">&lt;</span>数据表名<span class="operator">&gt;</span> <span class="keyword">where</span> 主键 <span class="operator">=</span> &quot;具体值&quot;;</span><br></pre></td></tr></table></figure>

<h3 id="索引"><a href="#索引" class="headerlink" title="索引"></a>索引</h3><h4 id="7-增加索引"><a href="#7-增加索引" class="headerlink" title="7. 增加索引"></a>7. 增加索引</h4><blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&gt;</span><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `project` (</span><br><span class="line"><span class="operator">&gt;</span>`p_id` <span class="type">varchar</span>(<span class="number">32</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line"><span class="operator">&gt;</span>`p_name` <span class="type">varchar</span>(<span class="number">64</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"><span class="operator">&gt;</span>`p_intr` text,</span><br><span class="line"><span class="operator">&gt;</span>`p_save` <span class="type">varchar</span>(<span class="number">512</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"><span class="operator">&gt;</span>`p_ctime` datetime <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"><span class="operator">&gt;</span><span class="keyword">PRIMARY</span> KEY (`p_id`)</span><br><span class="line"><span class="operator">&gt;</span>) ENGINE<span class="operator">=</span>InnoDB <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8mb4</span><br></pre></td></tr></table></figure>
</blockquote>
<ul>
<li>对表中字段加主键索引：</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> `project` <span class="keyword">ADD</span> <span class="keyword">PRIMARY</span> KEY ( `p_id` )</span><br></pre></td></tr></table></figure>

<ul>
<li>添加普通索引</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> `project` <span class="keyword">ADD</span> INDEX index_name ( `p_ctime` )</span><br></pre></td></tr></table></figure>


<ul>
<li>对表中字段加组合(多列)索引：</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> `project` <span class="keyword">ADD</span> INDEX select_by_id_name KEY ( `p_id`, `p_name` )</span><br></pre></td></tr></table></figure>

<ul>
<li>添加唯一索引</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> `project` <span class="keyword">ADD</span> <span class="keyword">UNIQUE</span> ( `p_save` )</span><br></pre></td></tr></table></figure>

<ul>
<li>添加全文索引</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> `project` <span class="keyword">ADD</span> FULLTEXT ( `p_intr` )</span><br></pre></td></tr></table></figure>


<h4 id="8-删除索引"><a href="#8-删除索引" class="headerlink" title="8. 删除索引"></a>8. 删除索引</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> INDEX id_n <span class="keyword">ON</span> student <span class="comment">-- or</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> student <span class="keyword">DROP</span> INDEX id;</span><br></pre></td></tr></table></figure>

<h2 id="改"><a href="#改" class="headerlink" title="改"></a>改</h2><h3 id="1-名称（ALTER）"><a href="#1-名称（ALTER）" class="headerlink" title="1. 名称（ALTER）"></a>1. 名称（ALTER）</h3><h4 id="修改表名"><a href="#修改表名" class="headerlink" title="修改表名"></a>修改表名</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> <span class="operator">&lt;</span>已有数据表名<span class="operator">&gt;</span> RENAME <span class="keyword">TO</span><span class="operator">/</span><span class="keyword">AS</span> <span class="operator">&lt;</span>新数据表名<span class="operator">&gt;</span>;</span><br></pre></td></tr></table></figure>

<h4 id="修改字段名"><a href="#修改字段名" class="headerlink" title="修改字段名"></a>修改字段名</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> <span class="operator">&lt;</span>数据表名<span class="operator">&gt;</span> CHANGE <span class="operator">&lt;</span>原有字段名<span class="operator">&gt;</span> <span class="operator">&lt;</span>已有字段名<span class="operator">&gt;</span> <span class="operator">&lt;</span>数据类型<span class="operator">&gt;</span>；</span><br></pre></td></tr></table></figure>

<h3 id="2-字段及其配置"><a href="#2-字段及其配置" class="headerlink" title="2. 字段及其配置"></a>2. 字段及其配置</h3><p><strong>修改字段默认值(ALTER)</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> <span class="operator">&lt;</span>已有数据表<span class="operator">&gt;</span> <span class="keyword">ALTER</span> <span class="operator">&lt;</span>字段<span class="operator">&gt;</span> <span class="keyword">SET</span> <span class="keyword">DEFAULT</span> <span class="operator">&lt;</span>默认值<span class="operator">&gt;</span>;</span><br></pre></td></tr></table></figure>

<p><strong>增加数据表字段(ALTER)</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> <span class="operator">&lt;</span>数据表名<span class="operator">&gt;</span> <span class="keyword">ADD</span> <span class="operator">&lt;</span>新字段名<span class="operator">&gt;</span> <span class="operator">&lt;</span>数据类型<span class="operator">&gt;</span> [……]；</span><br></pre></td></tr></table></figure>

<p><strong>修改数据类型(ALTER)</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> <span class="operator">&lt;</span>数据表名<span class="operator">&gt;</span> MODIFY <span class="operator">&lt;</span>已有字段名<span class="operator">&gt;</span> <span class="operator">&lt;</span>数据类型<span class="operator">&gt;</span>；</span><br><span class="line"></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> student MODIFY class_no <span class="type">VARCHAR</span>(<span class="number">100</span>);</span><br></pre></td></tr></table></figure>

<p><strong>修改字段对应数据值(UPDATE)</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">UPDATE</span> student</span><br><span class="line"><span class="keyword">SET</span> student_id<span class="operator">=</span><span class="number">8</span></span><br><span class="line"><span class="keyword">WHERE</span> college_name <span class="operator">=</span> <span class="string">&#x27;1&#x27;</span></span><br><span class="line"><span class="keyword">and</span> class_no <span class="operator">=</span> <span class="number">1</span>;</span><br></pre></td></tr></table></figure>

<p><strong>修改字段普通限制(ALTER)</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> student1(</span><br><span class="line">	student_id <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;学号&#x27;</span>,</span><br><span class="line">    name <span class="type">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">&#x27;姓名&#x27;</span></span><br><span class="line">)engine<span class="operator">=</span>innodb charset utf8mb4 comment <span class="string">&#x27;学生基本信息表&#x27;</span>; </span><br><span class="line"></span><br><span class="line">#<span class="number">1.</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> student1</span><br><span class="line"><span class="keyword">ADD</span> <span class="keyword">CONSTRAINT</span> showtime <span class="keyword">CHECK</span> (student_id <span class="operator">&gt;</span> <span class="number">0</span> <span class="keyword">AND</span> name <span class="operator">=</span> <span class="string">&#x27;Timmy&#x27;</span>); <span class="comment">-- 具体设置字段的限制</span></span><br><span class="line"></span><br><span class="line">#<span class="number">2.</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> student MODIFY class_no <span class="type">bigint</span> <span class="keyword">not</span> <span class="keyword">null</span>; <span class="comment">-- 调整为非空限制</span></span><br><span class="line"></span><br><span class="line">#<span class="number">3.</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> student MODIFY class_no <span class="type">bigint</span> <span class="keyword">null</span>;  <span class="comment">-- 调整为可空限制</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">table</span> student1;  <span class="comment">-- 查看各字段限制的详细信息</span></span><br></pre></td></tr></table></figure>

<h3 id="3-全局配置"><a href="#3-全局配置" class="headerlink" title="3. 全局配置"></a>3. 全局配置</h3><p><strong>修改数据库的全局特性(ALTER)</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> DATABASE student</span><br><span class="line"><span class="keyword">DEFAULT</span> <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb4</span><br><span class="line"><span class="keyword">DEFAULT</span> <span class="keyword">COLLATE</span> utf8mb4_0900_ai_ci;</span><br></pre></td></tr></table></figure>

<p><strong>调整数据库安全配置——可以更新和删除数据(SET)</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> variables <span class="keyword">like</span> <span class="string">&#x27;SQL_SAFE_UPDATES&#x27;</span>; <span class="comment">-- 查看当前数据库权限</span></span><br><span class="line"><span class="keyword">SET</span> SQL_SAFE_UPDATES <span class="operator">=</span> <span class="number">0</span>; <span class="comment">-- &#x27;0&#x27;代表可以修改，&#x27;1&#x27;代表禁止修改</span></span><br></pre></td></tr></table></figure>

<p><strong>调整主键的增长位置</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> student AUTO_INCREMENT<span class="operator">=</span><span class="number">11</span>; <span class="comment">-- 从11位置开始增长（11已经存在）</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> student_score AUTO_INCREMENT<span class="operator">=</span><span class="number">0</span>; <span class="comment">-- 用于初始化</span></span><br></pre></td></tr></table></figure>

<h2 id="查"><a href="#查" class="headerlink" title="查"></a>查</h2><h3 id="用户"><a href="#用户" class="headerlink" title="用户"></a>用户</h3><h4 id="查看所有的用户"><a href="#查看所有的用户" class="headerlink" title="查看所有的用户"></a>查看所有的用户</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="keyword">user</span> <span class="keyword">FROM</span> mysql.user;</span><br></pre></td></tr></table></figure>

<h3 id="数据库"><a href="#数据库" class="headerlink" title="数据库"></a>数据库</h3><h4 id="查看所有的数据库"><a href="#查看所有的数据库" class="headerlink" title="查看所有的数据库"></a>查看所有的数据库</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> databases;</span><br></pre></td></tr></table></figure>

<h4 id="查看当前使用的数据库"><a href="#查看当前使用的数据库" class="headerlink" title="查看当前使用的数据库"></a>查看当前使用的数据库</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">Select</span> Database();</span><br></pre></td></tr></table></figure>

<h4 id="查看MySQL版本信息"><a href="#查看MySQL版本信息" class="headerlink" title="查看MySQL版本信息"></a>查看MySQL版本信息</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> version()</span><br></pre></td></tr></table></figure>

<h3 id="表-1"><a href="#表-1" class="headerlink" title="表"></a>表</h3><h4 id="查看所有的表"><a href="#查看所有的表" class="headerlink" title="查看所有的表"></a>查看所有的表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> tables;</span><br></pre></td></tr></table></figure>

<h3 id="字段-1"><a href="#字段-1" class="headerlink" title="字段"></a>字段</h3><h4 id="查看所有的字段"><a href="#查看所有的字段" class="headerlink" title="查看所有的字段"></a>查看所有的字段</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">full</span> columns <span class="keyword">from</span> student;</span><br></pre></td></tr></table></figure>

<h4 id="查看所有字段浅层配置"><a href="#查看所有字段浅层配置" class="headerlink" title="查看所有字段浅层配置"></a>查看所有字段浅层配置</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> student;</span><br></pre></td></tr></table></figure>

<h4 id="查看每列字段的具体配置信息"><a href="#查看每列字段的具体配置信息" class="headerlink" title="查看每列字段的具体配置信息"></a>查看每列字段的具体配置信息</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">desc</span> student; <span class="comment">-- or</span></span><br><span class="line"><span class="keyword">show</span> columns <span class="keyword">from</span> student</span><br></pre></td></tr></table></figure>

<h4 id="查看指定字段"><a href="#查看指定字段" class="headerlink" title="查看指定字段"></a>查看指定字段</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 要查询的列名称</span><br><span class="line"><span class="keyword">FROM</span> 表名</span><br><span class="line">[<span class="keyword">WHERE</span> 满足的行条件]</span><br><span class="line">[<span class="keyword">GROUP</span> <span class="keyword">BY</span> 分组的依据] <span class="comment">---- 查询结果只显示每组的一条记录</span></span><br><span class="line">[<span class="keyword">ORDER</span> <span class="keyword">BY</span> 排序的依据] <span class="comment">---- order by 对查询结果排序 -&gt; desc 按降序排列；默认asc按升序排列</span></span><br><span class="line">[LIMIT 限定输出结果];	<span class="comment">---- 限定输出行数  limit 2,2; -&gt; #从第3条数据开始查询2条</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> students <span class="keyword">order</span> <span class="keyword">by</span> age <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> sid,sname,age,phone <span class="keyword">from</span> students <span class="keyword">group</span> <span class="keyword">by</span> age;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> students <span class="keyword">order</span> <span class="keyword">by</span> age limit <span class="number">2</span>,<span class="number">2</span>;</span><br></pre></td></tr></table></figure>

<h3 id="索引-1"><a href="#索引-1" class="headerlink" title="索引"></a>索引</h3><h4 id="查看所有的索引"><a href="#查看所有的索引" class="headerlink" title="查看所有的索引"></a>查看所有的索引</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> index <span class="keyword">from</span> student;</span><br></pre></td></tr></table></figure>

<h4 id="查看所有的主键"><a href="#查看所有的主键" class="headerlink" title="查看所有的主键"></a>查看所有的主键</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> keys <span class="keyword">from</span> student;</span><br></pre></td></tr></table></figure>

<h4 id="查看字符集和校对规则"><a href="#查看字符集和校对规则" class="headerlink" title="查看字符集和校对规则"></a>查看字符集和校对规则</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="type">character</span> <span class="keyword">set</span>; <span class="comment">--字符集规则 charset </span></span><br><span class="line"><span class="keyword">show</span> <span class="keyword">collation</span>; <span class="comment">-- 校对规则 collate</span></span><br></pre></td></tr></table></figure>

<h2 id="外键"><a href="#外键" class="headerlink" title="#外键"></a>#外键</h2><h4 id="创建表成功后，再添加外键约束"><a href="#创建表成功后，再添加外键约束" class="headerlink" title="创建表成功后，再添加外键约束"></a>创建表成功后，再添加外键约束</h4><p>外键用于与另一张表的<strong>关联</strong>。是能确定另一张表记录的字段，用于保持数据的一致性。</p>
<ul>
<li>一个表中的 FOREIGN KEY 指向另一个表中的 UNIQUE KEY</li>
<li>FOREIGN KEY 约束用于预防破坏表之间连接的行为</li>
<li>FOREIGN KEY 约束能防止非法数据插入外键列，因为它必须是它指向的那个表中的值之一</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 年级表</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `grade` (</span><br><span class="line">  `id` <span class="type">INT</span>(<span class="number">30</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT COMMENT <span class="string">&#x27;年级id&#x27;</span>,</span><br><span class="line">  `name` <span class="type">VARCHAR</span>(<span class="number">50</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;年级名称&#x27;</span>,</span><br><span class="line">  <span class="keyword">PRIMARY</span> KEY (`id`)</span><br><span class="line">)ENGINE<span class="operator">=</span>INNODB <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 学生表</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> `student` (</span><br><span class="line">  `id` <span class="type">INT</span>(<span class="number">4</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT COMMENT <span class="string">&#x27;学号&#x27;</span>,</span><br><span class="line">  `name` <span class="type">VARCHAR</span>(<span class="number">30</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">&#x27;匿名&#x27;</span> COMMENT <span class="string">&#x27;姓名&#x27;</span>,</span><br><span class="line">  `email` <span class="type">VARCHAR</span>(<span class="number">30</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;邮箱&#x27;</span>,</span><br><span class="line">  `id_grade` <span class="type">INT</span>(<span class="number">30</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;年级id&#x27;</span>,</span><br><span class="line">  <span class="keyword">PRIMARY</span> KEY(`id`)</span><br><span class="line">) ENGINE<span class="operator">=</span>INNODB <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8</span><br><span class="line"></span><br><span class="line"><span class="comment">-- DROP TABLE `grade`;</span></span><br><span class="line"><span class="comment">-- DROP TABLE `student`;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建表的时候没有外键关系</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> `student` </span><br><span class="line"><span class="keyword">ADD</span> <span class="keyword">CONSTRAINT</span> `fk_id_grade` <span class="keyword">FOREIGN</span> KEY (`id_grade`) <span class="keyword">REFERENCES</span> `grade` (`id`);</span><br><span class="line"><span class="comment">-- ALTER TABLE 表 ADD CONSTRAINT 约束名 FOREIGN KEY (作为外键的列) REFERENCES 哪个表 (哪个字段)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">--撤销 FOREIGN KEY 约束</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> `student`</span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">CONSTRAINT</span> `fk_id_grade`</span><br></pre></td></tr></table></figure>



<h4 id="参考资料合集"><a href="#参考资料合集" class="headerlink" title="参考资料合集"></a>参考资料合集</h4><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/chenlu5201314/article/details/100521863">1062 - Duplicate entry ‘1-1’ for key ‘PRIMARY’</a></p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/mayytest1202/p/10330208.html">MySQL：SELECT 语句（六） CONCAT() 函数的使用</a></p>
<p>Index</p>
<p><a target="_blank" rel="noopener" href="https://www.cda.cn/discuss/post/details/607d22085bf7943333002abf">MySQL报错Duplicate index ‘ename’ defined on the table ‘test.emp’.</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_41573234/article/details/80328281">MySQL——删除索引</a></p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/jianmingyuan/p/6740090.html">MySQL索引详解及优化(key和index区别)</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/JockLiu/article/details/97290667">MySQL||唯一约束（Unique Key）和非空约束（NOT NULL）</a></p>
<p>全局配置</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_42894018/article/details/81463085">关于MySQL安装成功后在cmd命令提示符下,mysql指令无效的解决方案</a></p>
<p>[Navicat for Mysql修改MySQL数据库密码](<a target="_blank" rel="noopener" href="https://www.cnblogs.com/zui-ai-java/p/10411000.html#:~:text=Navicat">https://www.cnblogs.com/zui-ai-java/p/10411000.html#:~:text=Navicat</a> for Mysql修改MySQL数据库密码，图文详解 1.创建一个连接 2.打开连接,3.按照图示123依次点击 4.输入新密码 5.查看实现修改密码功能的SQL语句（此步骤非必须） 6.最关键的一步：点击保存 7.出现如下现象，恭喜你，修改密码成功！)</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_45682496/article/details/115862243">Mysql设置主键自增，删除数据后，主键id依然从删除位置增加</a></p>
<p>Table</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/THISFOREVERYONE/article/details/107000266">MySQL查看表的字段，数据类型以及是否允许非空等</a></p>
<p>[MySQL怎么删除某一字段的所有值](<a target="_blank" rel="noopener" href="https://www.php.cn/mysql-tutorials-459616.html#:~:text=mysql%E5%88%A0%E9%99%A4%E6%9F%90%E4%B8%80%E5%AD%97%E6%AE%B5%E7%9A%84%E6%89%80%E6%9C%89%E5%80%BC%E7%9A%84%E6%96%B9%E6%B3%95%EF%BC%9A%E6%89%A7%E8%A1%8C[update">https://www.php.cn/mysql-tutorials-459616.html#:~:text=mysql删除某一字段的所有值的方法：执行[update</a> table_name set field_name %3D ‘’]命令即可。,如果要删除字段则执行[ALTER TABLE table_name DROP COLUMN field]命令。)</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_29229567/article/details/82743645">MYSQL：如何清空表中的数据</a></p>
<p><a target="_blank" rel="noopener" href="http://c.biancheng.net/view/7227.html">MySQL注释：单行注释和多行注释</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://zhuoshi--li.gitee.io/2022/09/10/Mysqlnew/" data-id="cl7urk99n0001houm8ymv3ev2" class="article-share-link">
        Share
      </a>
      
    </footer>

  </div>

  

  

</article>
    
    <article id="post-JAVA" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
      
  
  <h2 class="article-title" itemprop="name">
    <a href="/2022/04/03/JAVA/">JAVA</a>
  </h2>
  
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2022/04/03/JAVA/" class="article-date">
  <time datetime="2022-04-03T13:25:31.000Z" itemprop="datePublished">2022-04-03</time>
</a>
      
    </div>
    

    

    <div class="article-entry" itemprop="articleBody">
      
      
        
      
      
        <p>##Java接口和抽象类<br><strong>接口</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">package edu.neuq.Inter;</span><br><span class="line"></span><br><span class="line">public interface DMFrame &#123;</span><br><span class="line">	//先抽象，再具体</span><br><span class="line">	//抽象的接口，或者抽象类设计好之后，就不要修改了，如果接口一定要修改的话，可以用默认方法</span><br><span class="line">	//代码只添加，不修改</span><br><span class="line">	abstract public void clean();</span><br><span class="line">	abstract public void train();</span><br><span class="line">	abstract public void test();</span><br><span class="line">	default public void t()&#123;</span><br><span class="line">	&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>抽象类</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">package edu.neuq.DM;</span><br><span class="line"></span><br><span class="line">public abstract class DMFrame &#123;//data mining 框架类</span><br><span class="line">	abstract public void clean();</span><br><span class="line">	abstract public void train();</span><br><span class="line">	abstract public void test();</span><br><span class="line">	public void t()&#123;</span><br><span class="line">	&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://zhuoshi--li.gitee.io/2022/04/03/JAVA/" data-id="cl7urk99q0003houmbcep8dbv" class="article-share-link">
        Share
      </a>
      
    </footer>

  </div>

  

  

</article>
    
    <article id="post-Algorithm" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
      
  
  <h2 class="article-title" itemprop="name">
    <a href="/index.html">Algorithm</a>
  </h2>
  
  

    </header>
    

    
    <div class="article-meta">
      <a href="/index.html" class="article-date">
  <time datetime="2022-03-05T16:00:00.000Z" itemprop="datePublished">2022-03-06</time>
</a>
      
    </div>
    

    

    <div class="article-entry" itemprop="articleBody">
      
      
        
      
      
        <h2 id="Code-amp-Algorithm"><a href="#Code-amp-Algorithm" class="headerlink" title="Code&amp;Algorithm"></a>Code&amp;Algorithm</h2><h3 id="General"><a href="#General" class="headerlink" title="General"></a>General</h3><ul>
<li>Data structure</li>
</ul>
<h4 id="Enumerate"><a href="#Enumerate" class="headerlink" title="Enumerate"></a>Enumerate</h4><h4 id="Law"><a href="#Law" class="headerlink" title="Law"></a>Law</h4><ul>
<li>the basic math theory</li>
</ul>
<h3 id="Special"><a href="#Special" class="headerlink" title="Special"></a>Special</h3><h4 id="Sort-amp-Recursion"><a href="#Sort-amp-Recursion" class="headerlink" title="Sort&amp;Recursion"></a>Sort&amp;Recursion</h4><h4 id="Graph-Theory"><a href="#Graph-Theory" class="headerlink" title="Graph Theory"></a>Graph Theory</h4><ul>
<li>BFS&#x2F;DFS</li>
</ul>
<h4 id="Dynamic"><a href="#Dynamic" class="headerlink" title="Dynamic"></a>Dynamic</h4><ul>
<li>dp</li>
</ul>
<h4 id="Game-theory"><a href="#Game-theory" class="headerlink" title="Game theory"></a>Game theory</h4>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://zhuoshi--li.gitee.io/index.html" data-id="cl7urk99j0000houm72v4cvip" class="article-share-link">
        Share
      </a>
      
<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Share/" rel="tag">Share</a></li></ul>

    </footer>

  </div>

  

  

</article>
    
  </article>
  

  
</section>
</div>
    <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>Hexo &copy; 2022</li>
      
        <li>
          
            <a href="https://beian.miit.gov.cn/" target="_blank"></a>
            
        </li>
      
      <li>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></li>
      <li>theme  <a target="_blank" rel="noopener" href="https://github.com/zhwangart/hexo-theme-ocean">Ocean</a></li>
    </ul>
    <p><ul class="list-inline">
  
  <li><i class="fe fe-bar-chart tooltip" data-tooltip="PV"></i> <span id="busuanzi_value_site_pv"></span></li>
  
  <li><i class="fe fe-smile-alt tooltip" data-tooltip="UV"></i> <span id="busuanzi_value_site_uv"></span></li>
  
</ul></p>
  </div>
</footer>
  </main>
  <aside class="sidebar">
    <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/hexo.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2022/03/06/Algorithm">Home</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/Archives">Archives</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/Gallery">Gallery</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/About">About</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="Search">
        <i class="fe fe-search"></i>
        Search
      </a>
    </li>
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      <div class="totop" id="totop">
  <i class="fe fe-rocket"></i>
</div>
    </li>
    <li class="nav-item">
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="fe fe-feed"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
  </aside>
  
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>



<script src="/fancybox/jquery.fancybox.min.js"></script>






<script src="/js/ocean.js"></script>

</body>

</html>